#!/bin/bash
#This utility written by MJV to back fill data from the Picarro
while read line; do
eval $line
done < Config/config
while read line; do
eval $line
done < resources/Picarro_drv/dms.cfg
#G2301 CO2 analyzer.
#TRAP CTRL+C
cleanup ()
{
	exit
	cat /dev/null > "/home/aqdms/CARBLog/.tmp_pic_bckpl_out"
}
trap cleanup 1 2 3 6 15
#OBTAIN THE IP ADDRESS OF THE PICARRO
ip_address=`cat resources/Picarro_drv/.usr1 | grep -o '\-d [0-9,.]*' | sed 's/-d //g'`
#VARIABLIZE THE CURRENT FOLDER
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#AT THE MOMENT, THE SCRIPT TAKES A MINIMUM OF 24 HOURS WORTH OF DATA
#bgDATE OBTAINS THE 00:00 hour of the begining day
bgDate=$(zenity --calendar --text "Please choose the starting date" --title "Begin date" --day `date '+%d'` --month `date '+%m'` --year `date '+%Y'`)
#edDATE OBTAINS THE 00:00 hour of the last day
edDate=$(zenity --calendar --text "Please choose the ending date" --title "End date" --day `date '+%d'` --month `date '+%m'` --year `date '+%Y'`)
#OBTAIN THE ASSIGNABLE DATE STRING OF THE BEGINING DAY
begin_time=`date -d "$bgDate" '+%s'`
#OBTAIN THE ASSIGNABLE DATE STRING OF THE ENDING DAY	
ending_time=`date -d "$edDate" '+%s'`
#ASSIGN/INITIALIZE A COUNTER VARIARBLE TO BE USED TO MULTIPLY BY 60 AS WE BACKPOLL
minutes=0
#INITIALIZE THE COUNTER VARIABLE WITH THE INPUT FROM THE USER
counter_time=${begin_time}
while [ "${counter_time}" -lt "${ending_time}" ]; do
	{
		#WHEN BACKPOLLING WE MUST DO [ ] TASKS:
		#	1) DETERMINE IF THE INFORMATION NEEDED FOR BACKPOLL (COUNTER TIME + 8 HOURS, WHICH WE VARIABALIZE AS ${lcl_hrs})
		#	   IS CURRENTLY STORED IN THE WRITE CACHE.
				
				#Generate a date string which contains all the target variables we need 
				declare -a targets=(`date -d"@${counter_time}" '+%m/%d/%Y %m%d%Y %H:%M'`)
				target_day=${targets[0]}
				target_file=${targets[1]}
				targets_time=${targets[2]}
				adjusted_counter_time=`echo "60*60*${lcl_hrs} + ${counter_time}" | bc`
		#	2) IF IT IS NOT, READ THE PERTINENT DATA FROM THE PICARRO ITSELF,
		#	   AND TRANSLATE THIS INTO THE FORMAT NEEDED BY THE PARSE ENGINE
				#Generate a date string which contains all the path variables we need
				declare -a paths=(`date -d"@${adjusted_counter_time}" '+%Y %m %d %H:%M:'`)
				path_year=${paths[0]}
				path_month=${paths[1]}
				path_day=${paths[2]}
				target_path_time=${paths[3]}
				lineinfo="${path_year}-${path_month}-${path_day} ${target_path_time}"
				echo "lineinfo is $lineinfo"
				if [ `grep "${lineinfo}" "${DIR}/.write_cache" | wc -l` -le "0" ]; then
				{
					echo "trying to append" 
					cat /home/aqdms/.gvfs/userdata\ on\ ${ip_address}/DataLog_User/${path_year}/${path_month}/${path_day}/CFADS[0-9,A-Z,a-z,-,]*.dat | awk '{ print $1" "$2";"$9";"$10";"$17";"$18";"$19";"$20";"$21";"$22";" }' | grep -v "DATE" | grep -v "\-\-" | sort >> ${DIR}/.write_cache
					#BECAUSE THE CLOCK ON THE PICARRO DICTATES THE DATE FOLDER IT USES (NOT THE STAMP OF THE DATA)
					#WE ALWAY GRAB THE TARGET DAY AS WELL AS THE FOLLOWING DAY
					trail_hours=`echo "${lcl_hrs} + 24" | bc`
					adjusted_trail_counter_time=`echo "60*60*${trail_hours} + ${counter_time}" | bc`
					declare -a trail_paths=(`date -d"@${adjusted_trail_counter_time}" '+%Y %m %d %H:%M:'`)
					path_year=${trail_paths[0]}
					path_month=${trail_paths[1]}
					path_day=${trail_paths[2]}
					target_path_time=${trail_paths[3]}
					cat /home/aqdms/.gvfs/userdata\ on\ ${ip_address}/DataLog_User/${path_year}/${path_month}/${path_day}/CFADS[0-9,A-Z,a-z,-,]*.dat | awk '{ print $1" "$2";"$9";"$10";"$17";"$18";"$19";"$20";"$21";"$22";" }' | grep -v "DATE" | grep -v "\-\-" | sort | uniq >> ${DIR}/.write_cache
				}
				fi
		#	3) THEN PARSE THE DATA INTO A TMP FILE FOR EVENTUAL DMS EXPORT
				bash ${DIR}/parse.sh "$target_day" "$targets_time" "DMS" "Picarro" >> /home/aqdms/CARBLog/.tmp_pic_bckpl_out 
				counter_time=`echo $counter_time + 60 | bc`
	}	
	done
#	4) TRANSLATE IT INTO WINDOWS FORMAT AND FTP IT OUT
	cat "/home/aqdms/CARBLog/.tmp_pic_bckpl_out" > "/home/aqdms/CARBLog/${saroad}_Picarro_Backpoll.MIN"
	sed -i "s/$/\r/g" "/home/aqdms/CARBLog/${saroad}_Picarro_Backpoll.MIN"
	#cat /dev/null > "/home/aqdms/CARBLog/.tmp_pic_bckpl_out"

exit



